package com.sujeet.learning.sort;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.sujeet.learning.common.utils.TwoTuple;

import static com.sujeet.learning.common.utils.BasicUtils.show;

public class Selection {

	public static void main(String[] args) {

		ArrayList<Integer> sample = new ArrayList<Integer>(Arrays.asList(10, 9,
				1, 2, 3, 11, 4, 5, 29, 6, 7, 100, 11));
		show("size: " + sample.size());
		selectionSort(sample);
		show(sample);
		
	}

	private static void selectionSort(final ArrayList<Integer> sample) {	

		for (int i = 0; i < sample.size()-1; i++) {
			List<Integer> sublist = sample.subList(i,
					sample.size());
			TwoTuple<Integer, Integer> minAndPos = minAndPos(sublist);
			Integer min = minAndPos.first;
			Integer pos = minAndPos.second;
			show("sublist:" + sublist);
			show("min value: " + min);
			show("position of min vlue: " + pos);
			Integer tmp = sublist.get(0);
			sublist.set(0, min);
			sublist.set(pos, tmp);			
		}
		
	}

	/*
	 * this is not a suitable way to do things -> non-cohesive. indexOf could have been done 
	 * in the calling place itself, just wanted to try the tuple idea
	 */
	private static TwoTuple<Integer, Integer> minAndPos(
			final List<Integer> sample) {
		
		Integer min = Integer.MAX_VALUE;
		Integer pos = 0;
		for (Integer item : sample) {
			if (min > item)
				min = item;
		}
		pos = sample.indexOf(min);
		return new TwoTuple<Integer, Integer>(min, pos);
		
	}

}
